<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class SetupFoodsTable extends Migration {

    /**
     * Run the migrations.
     *:
     * @return void
     */
    public function up()
    {
        //

        // Creates the users table
        Schema::create('foods', function ($table) {
            $table->increments('id');
            $table->string('name',50)->nullable();
            $table->integer('menu_id')->unsigned()->default(0)->comment('菜单ID');
            $table->boolean('is_package')->default(false)->comment('是否为套餐');
            $table->smallInteger('is_top')->default(0)->comment('是否为推荐');
            $table->decimal('original_price',6,2)->default(0)->comment('原价');
            $table->decimal('price', 6, 2)->default(0)->comment('现价');
            $table->integer('count')->unsigned()->default(0)->comment('数量');
            $table->smallInteger('status')->default(0)->comment('状态：销售中，停止销售，已缺货，准备销售');
            $table->text('description')->comment('描述');
            $table->timestamps();
            $table->foreign('menu_id')->references('id')->on('menus');

            $table->index('menu_id');
            $table->index('is_package');
            $table->index('original_price');
            $table->index('price');
            $table->index('count');
            $table->index('status');

        });

        Schema::create('package_foods', function ($table) {
            $table->increments('id');
            $table->integer('food_id')->unsigned()->default(0)->comment('菜单ID');
            $table->integer('package_id')->unsigned()->default(0)->comment('套餐ID');
            $table->foreign('food_id')->references('id')->on('foods');
            $table->foreign('package_id')->references('id')->on('foods');

            $table->index('food_id');
            $table->index('package_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::table('package_foods', function(Blueprint $table) {
            $table->dropForeign('package_foods_food_id_foreign');
            $table->dropForeign('package_foods_package_id_foreign');
        });
        Schema::drop('package_foods');

        Schema::table('foods', function(Blueprint $table) {
            $table->dropForeign('foods_menu_id_foreign');
        });
        Schema::drop('foods');
    }

}
